﻿namespace Sidvall.WorkItemManager.UI.Entities
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public sealed partial class WorkItemSensor : global::Sidvall.WorkItemManager.UI.Entities.Core.BusinessBase, global::Sidvall.WorkItemManager.Data.IWorkItemSensor
    {
        #region Business Members

        #region Description

        private string _Description = string.Empty;
        partial void OnBeforeDescriptionPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeDescriptionPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterDescriptionPropertyChanged();
        public string Description
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Description);
                OnBeforeDescriptionPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Description);
                OnBeforeDescriptionPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Description", eventData.Value, true, true, true);
                    OnAfterDescriptionPropertyChanged();
                }
            }
        }

        #endregion
        #region Name

        private string _Name = string.Empty;
        partial void OnBeforeNamePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeNamePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterNamePropertyChanged();
        public string Name
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Name);
                OnBeforeNamePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Name);
                OnBeforeNamePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Name", eventData.Value, true, true, true);
                    OnAfterNamePropertyChanged();
                }
            }
        }

        #endregion
        #region Value

        private string _Value = string.Empty;
        partial void OnBeforeValuePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeValuePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterValuePropertyChanged();
        public string Value
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Value);
                OnBeforeValuePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Value);
                OnBeforeValuePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Value", eventData.Value, true, true, true);
                    OnAfterValuePropertyChanged();
                }
            }
        }

        #endregion
        #region ValueTimestamp

        private System.Nullable<System.DateTime> _ValueTimestamp = null;
        partial void OnBeforeValueTimestampPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<System.DateTime>> eventData);
        partial void OnBeforeValueTimestampPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<System.DateTime>> eventData);
        partial void OnAfterValueTimestampPropertyChanged();
        public System.Nullable<System.DateTime> ValueTimestamp
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<System.DateTime>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<System.DateTime>>(Sidvall.Business.ValidationManager.GetValidDateTime(_ValueTimestamp));
                OnBeforeValueTimestampPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<System.DateTime>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<System.DateTime>>(value, _ValueTimestamp);
                OnBeforeValueTimestampPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ValueTimestamp", eventData.Value, true, true, true);
                    OnAfterValueTimestampPropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItemId

        private long _WorkItemId = 0L;
        partial void OnBeforeWorkItemIdPropertyGet(Sidvall.Business.PropertyEventData<long> eventData);
        partial void OnBeforeWorkItemIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<long> eventData);
        partial void OnAfterWorkItemIdPropertyChanged();
        public long WorkItemId
        {
            get
            {
                Sidvall.Business.PropertyEventData<long> eventData = new Sidvall.Business.PropertyEventData<long>(_WorkItemId);
                OnBeforeWorkItemIdPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<long> eventData = new Sidvall.Business.PropertyChangeEventData<long>(value, _WorkItemId);
                OnBeforeWorkItemIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("WorkItemId", eventData.Value, true, true, true);
                    OnAfterWorkItemIdPropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItemSensorId

        private System.Nullable<long> _WorkItemSensorId = null;
        partial void OnBeforeWorkItemSensorIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<long>> eventData);
        partial void OnBeforeWorkItemSensorIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<long>> eventData);
        partial void OnAfterWorkItemSensorIdPropertyChanged();
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public System.Nullable<long> WorkItemSensorId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<long>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<long>>(_WorkItemSensorId);
                OnBeforeWorkItemSensorIdPropertyGet(eventData);
                return eventData.Value;
            }
            internal set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<long>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<long>>(value, _WorkItemSensorId);
                OnBeforeWorkItemSensorIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("WorkItemSensorId", eventData.Value, true, true, true);
                    OnAfterWorkItemSensorIdPropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItem

        private WorkItem _WorkItem;
        partial void OnBeforeWorkItemPropertyChanged(Sidvall.Business.PropertyChangeEventData<WorkItem> eventData);
        partial void OnAfterWorkItemPropertyChanged();
        public WorkItem WorkItem
        {
            get
            {
                return _WorkItem;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<WorkItem> eventData = new Sidvall.Business.PropertyChangeEventData<WorkItem>(value, _WorkItem);
                OnBeforeWorkItemPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("WorkItem", eventData.Value, true, true, false);
                    OnAfterWorkItemPropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItemSensorLogs

        private WorkItemSensorLogCollection _WorkItemSensorLogs;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public WorkItemSensorLogCollection WorkItemSensorLogs
        {
            get
            {
                return _WorkItemSensorLogs;
            }
            internal set
            {
                LoadInstanceField("WorkItemSensorLogs", value, true, false, false);
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected override string DataRowContextStorageKey
        {
            get
            {
                return global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.GetKey(this);
            }
        }

        #endregion
        #region DataRowIsValid

        public override bool DataRowIsValid(Sidvall.Data.BusinessRuleFilter filter)
        {
            return global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.DataRowIsValid(this, filter);
        }

        #endregion
        #region DataRowStateIsDirty

        public override bool DataRowStateIsDirty
        {
            get
            {
                return global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.DataRowStateIsDirty(this);
            }
        }

        #endregion

        #region LoadWorkItemSensorLogs

        public void LoadWorkItemSensorLogs(WorkItemSensorLogCollection value)
        {
            LoadInstanceField("WorkItemSensorLogs", value, true, true, false);
        }

        #endregion
        #region LoadWorkItemSensorLogsAsync

        public async System.Threading.Tasks.Task<WorkItemSensorLogCollection> LoadWorkItemSensorLogsAsync()
        {
            return await LoadWorkItemSensorLogsAsync(false, true, null, null, null);
        }
        public async System.Threading.Tasks.Task<WorkItemSensorLogCollection> LoadWorkItemSensorLogsAsync(bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchListCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemSensorLogField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemSensorLogRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemSensorManager.LoadWorkItemSensorLogsAsync(this, refresh, raisePropertyChanged, criteria, strategyContext, objectParameters);

            return this.WorkItemSensorLogs;
        }

        #endregion
        #region LoadWorkItemAsync

        public async System.Threading.Tasks.Task<WorkItem> LoadWorkItemAsync()
        {
            return await LoadWorkItemAsync(false, true, null, null, null);
        }
        public async System.Threading.Tasks.Task<WorkItem> LoadWorkItemAsync(bool refresh, bool raisePropertyChanged, global::Sidvall.Data.FetchCriteria<global::Sidvall.WorkItemManager.Data.Fields.WorkItemField, global::Sidvall.WorkItemManager.Data.Relations.WorkItemRelation> criteria, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemSensorManager.LoadWorkItemAsync(this, refresh, raisePropertyChanged, criteria, strategyContext, objectParameters);

            return this.WorkItem;
        }
        public WorkItem LoadWorkItem(WorkItem item)
        {
            LoadInstanceField("WorkItem", item, true, true, false);
            return item;
        }

        #endregion
        #region LoadInstanceField

        public void LoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.LoadInstanceField(this, fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region AcceptChanges

        public void AcceptChanges()
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.AcceptChanges(this);
        }

        #endregion
        #region Clone

        public WorkItemSensor Clone()
        {
            var copySettings = Sidvall.Data.CopySettings.Clone();
            return Copy(copySettings);
        }

        #endregion
        #region Copy

        public WorkItemSensor Copy()
        {
            return Copy(null);
        }
        public WorkItemSensor Copy(Sidvall.Data.CopySettings copySettings)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(this, copySettings.RowFilter))
                return null;
            var item = new WorkItemSensor();
            Import(item, this, copySettings);
            return item;
        }

        #endregion
        #region Import

        public void Import(global::Sidvall.WorkItemManager.Data.IWorkItemSensor item)
        {
            Import(item, null);
        }
        public void Import(global::Sidvall.WorkItemManager.Data.IWorkItemSensor item, Sidvall.Data.CopySettings copySettings)
        {
            Import(this, item, copySettings);
        }
        partial void OnAfterImport(WorkItemSensor destination, global::Sidvall.WorkItemManager.Data.IWorkItemSensor source, Sidvall.Data.CopySettings copySettings);
        private void Import(WorkItemSensor destination, global::Sidvall.WorkItemManager.Data.IWorkItemSensor source, Sidvall.Data.CopySettings copySettings)
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.Import(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings);
        }
        partial void OnAfterImport(WorkItemSensor destination, WorkItemSensor source, Sidvall.Data.CopySettings copySettings);
        private void Import(WorkItemSensor destination, WorkItemSensor source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source as global::Sidvall.WorkItemManager.Data.IWorkItemSensor, copySettings);
            OnAfterImport(destination, source, copySettings);
        }

        #endregion
        #region SaveItemAsync

        public async System.Threading.Tasks.Task<WorkItemSensor> SaveItemAsync()
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemSensorManager.SaveItemAsync(this);
        }
        public async System.Threading.Tasks.Task<WorkItemSensor> SaveItemAsync(Sidvall.Data.SaveMode mode)
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemSensorManager.SaveItemAsync(this, mode);
        }
        public async System.Threading.Tasks.Task<WorkItemSensor> SaveItemAsync(Sidvall.Data.SaveSettings settings)
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemSensorManager.SaveItemAsync(this, settings);
        }
        public async System.Threading.Tasks.Task<WorkItemSensor> SaveItemAsync(Sidvall.Data.SaveSettings settings, string strategyContext, global::Sidvall.Data.ObjectParameterCollection objectParameters)
        {
            return await global::Sidvall.WorkItemManager.UI.SystemContext.Current.DataPortal.WorkItemSensorManager.SaveItemAsync(this, settings, strategyContext, objectParameters);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync()
        {
            return await ValidateAsync((Sidvall.Data.ValidationSettings)null);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.ValidateAsync(this, context);
            await ValidateAsync(context);
            return context.AllBusinessRules;
        }
        partial void OnAfterValidate(Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        public async System.Threading.Tasks.Task ValidateAsync(Sidvall.Business.ValidationContext context)
        {
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnAfterValidate(itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync();
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = this as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }

        #endregion
        #region Initialize

        public void Initialize()
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.Initialize(this);
        }
        public void Initialize(Sidvall.Data.InitializeSettings settings)
        {
            global::Sidvall.WorkItemManager.UI.SystemContext.Current.EntityFacade.WorkItemSensorManager.Initialize(this, settings);
        }

        #endregion
        
        #region IWorkItemSensor Members

        string global::Sidvall.WorkItemManager.Data.IWorkItemSensor.Description
        {
            get
            {
                return this._Description;
            }
            set
            {
                this._Description = value;
            }
        }
        string global::Sidvall.WorkItemManager.Data.IWorkItemSensor.Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }
        string global::Sidvall.WorkItemManager.Data.IWorkItemSensor.Value
        {
            get
            {
                return this._Value;
            }
            set
            {
                this._Value = value;
            }
        }
        System.Nullable<System.DateTime> global::Sidvall.WorkItemManager.Data.IWorkItemSensor.ValueTimestamp
        {
            get
            {
                return this._ValueTimestamp;
            }
            set
            {
                this._ValueTimestamp = value;
            }
        }
        long global::Sidvall.WorkItemManager.Data.IWorkItemSensor.WorkItemId
        {
            get
            {
                return this._WorkItemId;
            }
            set
            {
                this._WorkItemId = value;
            }
        }
        System.Nullable<long> global::Sidvall.WorkItemManager.Data.IWorkItemSensor.WorkItemSensorId
        {
            get
            {
                return this._WorkItemSensorId;
            }
            set
            {
                this._WorkItemSensorId = value;
            }
        }
        global::Sidvall.WorkItemManager.Data.IWorkItem global::Sidvall.WorkItemManager.Data.IWorkItemSensor.WorkItem
        {
            get
            {
                return this._WorkItem;
            }
            set
            {
                UpdateChildProperty(this._WorkItem, value);
                this._WorkItem = value as WorkItem;
            }
        }
        System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemSensorLog> global::Sidvall.WorkItemManager.Data.IWorkItemSensor.WorkItemSensorLogs
        {
            get
            {
                return this._WorkItemSensorLogs;
            }
        }
        void global::Sidvall.WorkItemManager.Data.IWorkItemSensor.SetWorkItemSensorLogs(Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemSensorLog> items)
        {
            UpdateChildProperty(this._WorkItemSensorLogs, items);
            this._WorkItemSensorLogs = items as WorkItemSensorLogCollection;
        }

        #endregion

        #endregion
        #region Constructors

        public WorkItemSensor()
        {
        }

        public WorkItemSensor(Sidvall.Data.DataRowState? dataRowState)
            : base(dataRowState)
        {
        }

        #endregion
    }
}